//
//  ECDatePicker.m
//  ECERP.CRM
//
//  Created by 刘思麟 on 2018/11/6.
//  Copyright © 2018 Mac. All rights reserved.
//

#import "ECDatePicker.h"

@interface ECDatePicker()

@property (nonatomic,strong) UIView * contentView;

@property (nonatomic,strong) UIDatePicker * datePicker;

@property (nonatomic,strong) UIView * toolView;

@property (nonatomic,strong) UIButton * makeSureBT;

@end

@implementation ECDatePicker

- (instancetype)init {
    if (self = [super init]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    WS(weakSelf, self);
    WS(weakWindow, ECAPPDelegate.effectWindow);
    self.contentView = [UIView new];
    self.contentView.backgroundColor = ECCOLOR(0xffffff);
    [ECAPPDelegate.effectWindow removeAllSubviews];
    [ECAPPDelegate.effectWindow addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(weakWindow);
        if (@available(iOS 11.0,*)) {
            make.top.mas_equalTo(weakWindow.mas_safeAreaLayoutGuideBottom).mas_offset(-261* PX_RELATIVE375);
        }else {
            make.top.mas_equalTo(weakWindow.mas_bottom).mas_offset(-261* PX_RELATIVE375);
        }
    }];
    
    self.toolView = [[UIView alloc] init];
    self.toolView.backgroundColor = ECCOLOR(0xcccccc);
    [self.contentView addSubview:self.toolView];
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(40*PX_RELATIVE375);
    }];
    
    self.makeSureBT = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.makeSureBT setTitleColor:ECCOLOR_NAVIBACK forState:UIControlStateNormal];
    [self.makeSureBT setTitle:@"确定" forState:UIControlStateNormal];
    [self.makeSureBT addTarget:self action:@selector(doSelected:) forControlEvents:UIControlEventTouchUpInside];
    self.makeSureBT.titleLabel.font = ECFONT(14 * PX_RELATIVE375);
    [self.toolView addSubview:self.makeSureBT];
    [self.makeSureBT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.toolView);
        make.right.mas_equalTo(weakSelf.toolView).mas_offset(-12*PX_RELATIVE375);
    }];
    
    self.datePicker = [[UIDatePicker alloc] init];
    [self.datePicker setLocale:[[NSLocale alloc]initWithLocaleIdentifier:@"zh-Hans"]];
    self.datePicker.datePickerMode = UIDatePickerModeDate;
    self.datePicker.date = [NSDate date];
    [self.contentView addSubview:self.datePicker];
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(weakSelf.toolView.mas_bottom);
        if (@available(iOS 11.0,*)) {
            make.bottom.mas_equalTo(weakSelf.contentView.mas_safeAreaLayoutGuideBottom);
        }else {
            make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
        }
    }];
}

- (void)show {
    WS(weakSelf, self);
    [ECAPPDelegate showEffectWindow:^(UIWindow * _Nonnull effectWindow) {
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.contentView.alpha = 1;
        }];
    }];
}

- (void)doSelected:(UIButton*)sender {
    WS(weakSelf, self);
    if (self.selectedDateBlock) {
        self.selectedDateBlock(self.datePicker.date);
    }
    [ECAPPDelegate hiddenEffectWindow:^(UIWindow * _Nonnull effectWindow) {
        [UIView animateWithDuration:0.25 animations:^{
            weakSelf.contentView.alpha = 0;
        }];
    }];
}

@end
